<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class QualityReport extends AdminController {

	function __construct() {
		parent::__construct();
		$this -> load-> model('Quality_report_model');
		$this -> load-> model('Teacher_info_model');
		$this -> load-> model('Attend_model');
		$this ->load->library('PHPWord/PHPWord');
		$this -> load-> model('Admin_model');
		$this -> load -> library("ProjectService/quality_report_service");
	}

	function Index() {
	
		$this->load->view('admin/qualityList');
	}
	function getDatas($params = array(), $isExport = false) {
		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> Quality_report_model -> pickPages($connar, $params);
		}

		$connar['orderby'] = "id asc";
		$connar['where']=$this->sqlEscape("db_quality_report.status=?? ",array(CommonStatus::UnDeleted[XPHEnum::Value]));
		$connar['join']=array('db_admin','db_admin.id=db_quality_report.creator');
		$connar['items']='db_quality_report.*,db_admin.username';
		// if(getSess()['role_id']==WebRole::)
		
		if (isset($params['param'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and (db_admin.username like '%??%' ",array($params['param']));
			// $connar['where'] .= $this->sqlLikeEscape(" or db_question.title like '%??%') ",array($params['param']));
		}

		if (!$isExport) {
			$data = $this -> Quality_report_model -> get_page_list($connar);

		} else {
			$data["ret"] = $this -> Quality_report_model -> get_list_full($connar);
		}
		foreach ($data['ret'] as $k => $v) {
			$data['ret'][$k]['created']=date('Y-m-d H:i:s',$v['created']);	
			$data['ret'][$k]['start_month']=date('Y-m',strtotime($v['start_month']));	
			$data['ret'][$k]['end_month']=date('Y-m',strtotime($v['end_month']));	
	
		}

		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}

	//新增，修改
	function edit($id){
		if($id == 0){
			$data['title'] = "新增报告";
			$data['detail'] = $this -> Quality_report_model -> get_single(array("id" => $id));
		}else{
			$data['title'] = "编辑报告";
			$data['detail'] = $this -> Quality_report_model -> get_single(array("id" => $id));
			// $data['detail']['start_month'] = date('Y-m',strtotime($data['detail']['start_month']));	
			// $data['detail']['end_month'] = date('Y-m',strtotime($data['detail']['end_month']));	
			$data['detail']['years'] = date('Y',strtotime($data['detail']['start_month']));	
			// $data['detail']['img_base64']="data:image/jpeg;base64,".base64_encode(base_url().$data['detail']['img']);
		}
		
		$this -> load -> view('admin/qualityEdit',$data);
	}

	function save(){
		// if($_POST['start_month'] == '' || $_POST['end_month'] == ''){
		// 	exit(retJson('起始年月/结束年月不能为空！', false));
		// }
		if($_POST['years'] == ''){
			exit(retJson('报告年份不能为空！', false));
		}

		// var_dump($_POST['years']);

		$start_date = date("Y-m-01", strtotime($_POST['years'].'-01')).' 00:00:00';//获取指定月的第一天
		$end_date = date("Y-m-t", strtotime($_POST['years'].'-12')).' 23:59:59';//获取指定月的最后一天
		
		// $dt = "2023-01";
		// echo '第一天 : '. date("Y-m-01", strtotime($dt)).' <br> 最后一天 : '. date("Y-m-t", strtotime($dt));
		$question['start_month']=$start_date;
		$question['end_month']=$end_date;
		// var_dump($start_date); 
		// var_dump($end_date); 

		if($_POST['id']==0){
			$question['created']=time();
			$question['status']=CommonStatus::UnDeleted[XPHEnum::Value];
			$question['creator']=getSess()['id'];			
			$this->Quality_report_model->add($question);
		}else{
			$question['id']=$_POST['id'];
			$this->Quality_report_model->update($question);
		}		
		
		exit(retJson('保存成功！',true));	
	}

	function delete($id){
		$this->Quality_report_model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
		exit(retJson('删除成功',true));
	}

	//导出word报告页面
	function exportQualityWord($quality_report_id){
		$data['title'] = "导出word报告";
		$data['quality_report_id'] =$quality_report_id;
		$Qualitydetail = $this -> Quality_report_model -> get_single(array("id" => $quality_report_id));
		$data['years_month'] = date("Y年m月",strtotime($Qualitydetail['start_month'])).' - '.date("Y年m月",strtotime($Qualitydetail['end_month']));
		$data['years'] = date("Y年",strtotime($Qualitydetail['start_month']));
		$Qualitydetail['listen_type'] = '日常听课';
		//--------表1统计开始---------
		$table1Arr = $this->quality_report_service->getTableList1($Qualitydetail);//获取表1的数据

		// echo '<pre>';
		// var_dump($table1Arr);
		// exit;
		//----------表格1数据赋值--------------
		$data['table1'] = $table1Arr['table1'];

		//--------表1统计结束---------
		//--------表2统计开始---------
		$attend_Arr = $this->quality_report_service->getTable2List(array('start_time'=>$Qualitydetail['start_month'],'end_time'=>$Qualitydetail['end_month'],'listen_type'=>$Qualitydetail['listen_type']));
		$attend_Arr['attend_count_Arr']['shangmao_xtkrs_count'] = isset($table1Arr['table11']['商贸学院'])?$table1Arr['table11']['商贸学院']:'0';
		$attend_Arr['attend_count_Arr']['sjyysxy_xtkrs_count']=isset($table1Arr['table11']['设计与艺术学院（腾讯云国际互联网学院）'])?$table1Arr['table11']['设计与艺术学院（腾讯云国际互联网学院）']:'0';
		//----------表格2数据赋值--------------
		$data['table2'] =$attend_Arr['table2'];
		$data['table2Total'] =$attend_Arr['table2Total'];
		$data['attend_count'] =$attend_Arr['attend_count_Arr'];
		$data['attend_count_list'] =$attend_Arr['new_c'];
		//--------表2统计结束---------
		//--------表3统计开始---------
		$table3Arr = $this->quality_report_service->getTableList3(array('start_time'=>$Qualitydetail['start_month'],'end_time'=>$Qualitydetail['end_month'],'listen_type'=>$Qualitydetail['listen_type']));//获取表1的数据
		$data['table3'] =$table3Arr['table3'];
		$data['table3_attend_count'] =$table3Arr['attend_count_Arr'];
		
		//--------表3统计结束---------
		//--------表4统计开始---------
		$table4Arr = $this->quality_report_service->getTableList4(array('start_time'=>$Qualitydetail['start_month'],'end_time'=>$Qualitydetail['end_month'],'listen_type'=>$Qualitydetail['listen_type']));

		$data['table4'] =$table4Arr['table4'];
		$data['table4contentStr'] =$table4Arr['contentStr'];
		//--------表4统计结束---------
		//--------表5统计开始---------
		$table5Arr = $this->quality_report_service->getTableList5(array('start_time'=>$Qualitydetail['start_month'],'end_time'=>$Qualitydetail['end_month'],'listen_type'=>$Qualitydetail['listen_type']));

		$data['table5'] =$table5Arr['table5'];
		$data['table5miaoshu'] =$table5Arr['table5miaoshu'];
		//--------表5统计结束---------

		//--------表6统计开始---------
		$table6Arr = $this->quality_report_service->getTableList6(array('start_time'=>$Qualitydetail['start_month'],'end_time'=>$Qualitydetail['end_month'],'listen_type'=>$Qualitydetail['listen_type']));

		$data['table6'] =$table6Arr['table6'];
// 		echo '<pre>';
// var_dump($data['table6']);
// 		exit;
		//--------表6统计结束---------

		//附件1
		$list1 = $this->quality_report_service ->fujian1($Qualitydetail);
		$data['fujianlist1'] =$list1;
		//附件2
		$list2 = $this->quality_report_service ->fujian2($Qualitydetail);
		$data['fujianlist2'] =$list2;
		//附件3
		$list3 = $this->quality_report_service ->fujian3($Qualitydetail);
		$data['fujianlist3'] =$list3;

		//附件4
		$list4 = $this->quality_report_service ->fujian4($Qualitydetail);
		$data['fujianlist4'] =$list4;

		//附件5
		$list5 =$this->quality_report_service ->fujian5($Qualitydetail);
		$data['fujianlist5'] =$list5;
		//附件6
		$list6 =$this->quality_report_service ->fujian6($Qualitydetail);
		$data['fujianlist6'] =$list6;
		//附件7鉴湖学院
		$list7 =$this->quality_report_service ->fujian7($Qualitydetail);
		$data['fujianlist7'] =$list7;
		//附件8
		$list8 =$this->quality_report_service ->fujian8($Qualitydetail);
		// var_dump($list8);
		$data['fujianlist8'] =$list8;

		// var_dump(strtotime($Qualitydetail['start_month']));
		// var_dump(strtotime($Qualitydetail['end_month']));

		$this->load->view('admin/exportQualityWord',$data);
	}




	//选择需要听课的人
	function chooseTeacher(){
		$data['title'] = "部门教师";
		
		$this->load->view('admin/chooseTeacher',$data);
	}

	function getchooseTeacher(){
		$connar['orderby'] = "id asc";
		$connar['items']='id,usercode,username,dept_name';
		$connar['where']=$this->sqlEscape("status=?? and usercode != '' and (role_id = ".WebRole::Teacher[XPHEnum::Value]." OR FIND_IN_SET(".WebRole::Teacher[XPHEnum::Value].", sub_role_id) ) ",array(CommonStatus::UnDeleted[XPHEnum::Value]));

		if($_POST['value'] != ""){
			$connar['where'] .= $this->sqlLikeEscape(" and (usercode like '%??%' ",array($_POST['value']));
			$connar['where'] .= $this->sqlLikeEscape(" or username like '%??%' ",array($_POST['value']));
			$connar['where'] .= $this->sqlLikeEscape(" or dept_name like '%??%') ",array($_POST['value']));
		}

		$adminlist= $this -> Admin_model -> get_list_full($connar);	
		$new_admin = array();
		foreach ($adminlist as $key => $value) {
			$new_admin[$value['dept_name']][] = $value;
			# code...
		}
		$i =0;
		foreach ($new_admin as $k => $v) {
			$module[$i]['pid']=0;
			$module[$i]['name']=$k;
			// $module[$i]['inputName']='module';
			
			foreach($v as $k1=>$v1){
				$v[$k1]['pid'] = ($i+1);
				$v[$k1]['name'] = $v1['username']."(".$v1['usercode'].")";
				$v[$k1]['inputName']='module';
				$v[$k1]['value']=$v1['username']."(".$v1['usercode'].")";
			}
			$module[$i]['children']=$v;
			$i++;
		}
		$module=listToTree($module,'name','pid','children');
		$module=array_values($module);

		echo json_encode($module);
		 
	}

	
	// function saveRoleModule(){
	// 	var_dump($_POST);
	// }

}
